package org.usp.poli.bipt.client.ui;


import java.util.Date;

import org.usp.poli.bipt.client.BrazilianIPT;
import org.usp.poli.bipt.client.GreetingService;
import org.usp.poli.bipt.client.GreetingServiceAsync;
import org.usp.poli.bipt.client.metadata.*;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;




public class MetaDataForm {
	
	private final GreetingServiceAsync greetingService = GWT
	.create(GreetingService.class);
	
	private Button cadastroBtn;
	private MetaData metaData = new MetaData();
	private GeographicCoverage geographicCoverage = new GeographicCoverage();
	private ResourceOriginator resourceOriginator = new ResourceOriginator();
	private BasicMetadataTypes basicMetadataTypes = new BasicMetadataTypes();
	private GeneralKeywords generalKeywords = new GeneralKeywords();
	private IPRights ipRights = new IPRights();
	private Methods methods = new Methods();
	private ResearchProject researchProject = new ResearchProject();
	private TaxonomicCoverage taxonomicCoverage = new TaxonomicCoverage();
	private TemporalCoverage temporalCoverage = new TemporalCoverage();
	private LowestCommonTax lowestCommonTax = new LowestCommonTax();
	
	//private PushButton basicMetadataNextBtn = new PushButton();
	//...
	
	
	public MetaDataForm() {
		
	}
	
	
	public Widget getBasicMetaData(){
		BrazilianIPT.fluxo = 1;
		final TextBox titleTB = new TextBox();
		final TextBox typesTB = new TextBox();
		final TextBox contactNameTB = new TextBox();
		final TextBox contactEmailTB = new TextBox();
		final TextBox uploadLogoTB = new TextBox();
		final TextBox descriptionTB = new TextBox();
		
		
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Basic Metadata</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Title");
	    layout.setWidget(1, 1, titleTB);
	    layout.setHTML(2, 0, "Types");
	    layout.setWidget(2, 1, typesTB);
	    layout.setHTML(3, 0, "Contact Name");
	    layout.setWidget(3, 1, contactNameTB);
	    layout.setHTML(4, 0, "Contact Email");
	    layout.setWidget(4, 1, contactEmailTB);
	    layout.setHTML(5, 0, "Upload Logo");
	    layout.setWidget(5, 1, uploadLogoTB);
	    layout.setHTML(6, 0, "Description");
	    layout.setWidget(6, 1, descriptionTB);
	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
	    
	    //getBasicMetadataNextBtn().setText("Next");
	   
	    cadastroBtn.addClickHandler(new ClickHandler() {
	    	
			
			@Override
			public void onClick(ClickEvent event) {
				

		    	final String key = "chave";      // ...
		    	byte[] value = null;    // ...
		    	
		    	
				
				metaData.setTitle(titleTB.getText());
				metaData.setTypes(typesTB.getText());
				metaData.setContactName(contactNameTB.getText());
				metaData.setContactEmail(contactEmailTB.getText());
				metaData.setUpdateLogo(uploadLogoTB.getText());
				metaData.setDescription(descriptionTB.getText());
				
				greetingService.setBasicMetaData(metaData,
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getBasicmetadataLk().setEnabled(false);
						BrazilianIPT.metadataMenu.getGeneralKeywordsLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getGeographicCoverageLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getIpRightsLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getLowestCommonTaxLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getMethodsLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getResearchProjectLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getTemporalCoverageLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getTaxonomicCoverageLk().setEnabled(true);
						BrazilianIPT.metadataMenu.getResourceOriginatorLk().setEnabled(true);
						
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
	    layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    	    
	    return decPanel;
	}
	public Widget getGeographicCoverage(){
		BrazilianIPT.fluxo = 2;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox latitudeTB = new TextBox();
		final TextBox longitudeTB = new TextBox();
		final TextBox radiusTB = new TextBox();
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Geographic Coverage</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Latitude");
	    layout.setWidget(1, 1, latitudeTB);
	    layout.setHTML(2, 0, "Longitude");
	    layout.setWidget(2, 1, longitudeTB);
	    layout.setHTML(3, 0, "Radius");
	    layout.setWidget(3, 1, radiusTB);
	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
	    	
			
			@Override
			public void onClick(ClickEvent event) {
				
				geographicCoverage.setLatitude(Integer.parseInt(latitudeTB.getText()));
				geographicCoverage.setLongitude(Integer.parseInt(longitudeTB.getText()));
				geographicCoverage.setRadius(Integer.parseInt(radiusTB.getText()));
				
				greetingService.setGeographicCoverage(geographicCoverage, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getGeographicCoverageLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	

	public Widget getLowestCommonTax(){
		BrazilianIPT.fluxo = 3;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox scientificNameTB = new TextBox();
		final TextBox commonNameTB = new TextBox();
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Lowest Common Tax</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	 // Add some standard form options
	    layout.setHTML(1, 0, "Scientific Name");
	    layout.setWidget(1, 1, scientificNameTB);
	    layout.setHTML(2, 0, "Common Name");
	    layout.setWidget(2, 1, commonNameTB);
	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				lowestCommonTax.setCommontName(commonNameTB.getText());
				lowestCommonTax.setScientificName(scientificNameTB.getText());
				
				greetingService.setLowestCommonTax(lowestCommonTax, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getLowestCommonTaxLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	
	public Widget getResearchProject(){
		BrazilianIPT.fluxo = 4;
		// Create a table to layout the form options
		final FlexTable layout = new FlexTable();
		final TextBox titleTB = new TextBox();
		final TextBox leadOganisationTB = new TextBox();
		final TextBox abstractTB = new TextBox();
	    final TextBox fundingTB = new TextBox();
		final TextBox studyAreaDescriptionTB = new TextBox();
		final TextBox designDescriptionTB = new TextBox();
	    
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Research Project</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Title");
	    layout.setWidget(1, 1, titleTB);
	    layout.setHTML(2, 0, "Lead Organisation");
	    layout.setWidget(2, 1, leadOganisationTB);
	    layout.setHTML(3, 0, "Abstract");
	    layout.setWidget(3, 1, abstractTB);
	    layout.setHTML(4, 0, "Funding");
	    layout.setWidget(4, 1, fundingTB);
	    layout.setHTML(5, 0, "Study Area Description");
	    layout.setWidget(5, 1, studyAreaDescriptionTB);
	    layout.setHTML(6, 0, "Design Description");
	    layout.setWidget(6, 1, designDescriptionTB);
	    	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				researchProject.setAbstract1(abstractTB.getText());
				researchProject.setDesignDescription(designDescriptionTB.getText());
				researchProject.setFunding(fundingTB.getText());
				researchProject.setLeadOganization(leadOganisationTB.getText());
				researchProject.setStudyAreaDescription(studyAreaDescriptionTB.getText());
				researchProject.setTitle(titleTB.getText());
				greetingService.setResearchProject(researchProject, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getResearchProjectLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	public Widget getMethods(){
		BrazilianIPT.fluxo = 5;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox methodsTB = new TextBox();
		final TextBox samplingDescriptionTB = new TextBox();
		final TextBox qualityControlTB = new TextBox();
	    
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Methods</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Methods");
	    layout.setWidget(1, 1, methodsTB);
	    layout.setHTML(2, 0, "Sampling Description");
	    layout.setWidget(2, 1, samplingDescriptionTB);
	    layout.setHTML(3, 0, "Quality Control");
	    layout.setWidget(3, 1, qualityControlTB);
	            
	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				methods.setMethods(methodsTB.getText());
				methods.setQualityControl(qualityControlTB.getText());
				methods.setSamplingDescription(samplingDescriptionTB.getText());
				greetingService.setMethods(methods, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getMethodsLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	
	public Widget getResourceOriginator(){
		BrazilianIPT.fluxo = 6;
		// Create a table to layout the form options
		final FlexTable layout = new FlexTable();
	    final TextBox resourceLanguageTB = new TextBox();
		final TextBox firstNameTB = new TextBox();
		final TextBox lastNameTB = new TextBox();
	    final TextBox organisationTB = new TextBox();
		final TextBox positionTB = new TextBox();
		final TextBox phoneTB = new TextBox();
		final TextBox emailTB = new TextBox();
		final TextBox postalCodeTB = new TextBox();
		final TextBox provinceTB = new TextBox();
	    final TextBox cityTB = new TextBox();
		final TextBox countryTB = new TextBox();
		final TextBox homePageTB = new TextBox();
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Resource Originator</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Resource Language");
	    layout.setWidget(1, 1, resourceLanguageTB);
	    layout.setHTML(2, 0, "First Name");
	    layout.setWidget(2, 1, firstNameTB);
	    layout.setHTML(3, 0, "Last Name");
	    layout.setWidget(3, 1, lastNameTB);
	    layout.setHTML(4, 0, "Organisation");
	    layout.setWidget(4, 1, organisationTB);
	    layout.setHTML(5, 0, "Position");
	    layout.setWidget(5, 1, positionTB);
	    layout.setHTML(6, 0, "Phone");
	    layout.setWidget(6, 1, phoneTB);
	    layout.setHTML(7, 0, "Email");
	    layout.setWidget(7, 1, emailTB);
	    layout.setHTML(8, 0, "HomePage");
	    layout.setWidget(8, 1, homePageTB);
	    layout.setHTML(9, 0, "Postal Code");
	    layout.setWidget(9, 1, postalCodeTB);
	    layout.setHTML(10, 0, "City");
	    layout.setWidget(10, 1, cityTB);
	    layout.setHTML(11, 0, "Province");
	    layout.setWidget(11, 1, provinceTB);
	    layout.setHTML(12, 0, "Country");
	    layout.setWidget(12, 1, countryTB);
	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				resourceOriginator.setResourceLanguage(resourceLanguageTB.getText());
				resourceOriginator.setCity(cityTB.getText());
				resourceOriginator.setCountry(countryTB.getText());
				resourceOriginator.setEmail(emailTB.getText());
				resourceOriginator.setFirstName(firstNameTB.getText());
				resourceOriginator.setHomepage(homePageTB.getText());
				resourceOriginator.setLastName(lastNameTB.getText());
				resourceOriginator.setOrganisation(organisationTB.getText());
				resourceOriginator.setPhone(Integer.parseInt(phoneTB.getText()));
				resourceOriginator.setPosition(positionTB.getText());
				resourceOriginator.setPostalCode(Integer.parseInt(postalCodeTB.getText()));
				resourceOriginator.setProvince(provinceTB.getText());
				
				greetingService.setResourceOriginator(resourceOriginator, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getResourceOriginatorLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(14, 0, cadastroBtn);
		cellFormatter.setColSpan(14, 0, 4);
		cellFormatter.setHorizontalAlignment(14, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	

	public Widget getTaxonomicCoverage(){
		BrazilianIPT.fluxo = 7;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox generalDescriptionTB = new TextBox();
		final TextBox taxonomicKeywordsTB = new TextBox();
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Taxonomic Coverage</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	 // Add some standard form options
	    layout.setHTML(1, 0, "General Description");
	    layout.setWidget(1, 1, generalDescriptionTB);
	    layout.setHTML(2, 0, "Taxonomic Keywords");
	    layout.setWidget(2, 1, taxonomicKeywordsTB);
	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				taxonomicCoverage.setGeneralDescription(generalDescriptionTB.getText());
				taxonomicCoverage.setTaxonomicKeywords(taxonomicKeywordsTB.getText());
				greetingService.setTaxonomicCoverage(taxonomicCoverage, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getTaxonomicCoverageLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	
	public Widget getIPRights(){
		BrazilianIPT.fluxo = 8;
		final FlexTable layout = new FlexTable();
	    final TextBox purposeTB = new TextBox();
		final TextBox maintanceTB = new TextBox();
		final TextBox ipRightsTB = new TextBox();
	    
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>IPRights</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    layout.setHTML(1, 0, "Porpose");
	    layout.setWidget(1, 1, purposeTB);
	    layout.setHTML(2, 0, "Maintance");
	    layout.setWidget(2, 1, maintanceTB);
	    layout.setHTML(3, 0, "IPRights");
	    layout.setWidget(3, 1, ipRightsTB);
	    	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ipRights.setIpRights(ipRightsTB.getText());
				ipRights.setMaintance(maintanceTB.getText());
				ipRights.setPurpose(purposeTB.getText());
				greetingService.setIPRights(ipRights, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getIpRightsLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}
	

	public Widget getGeneralKeywords(){
		BrazilianIPT.fluxo = 9;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox keywordsTB = new TextBox();
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>General Keywords</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	 // Add some standard form options
	    layout.setHTML(1, 0, "Keywords");
	    layout.setWidget(1, 1, keywordsTB);
	   	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				generalKeywords.setKeywords(keywordsTB.getText());
				greetingService.setGeneralKeywords(generalKeywords, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getGeneralKeywordsLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}


	public Widget getTemporalCoverage(){
		BrazilianIPT.fluxo = 10;
		// Create a table to layout the form options
	    final FlexTable layout = new FlexTable();
	    final TextBox startDateTB = new TextBox();
		final TextBox endDateTB = new TextBox();


		Date data = new Date();
		
		
		
	    layout.setCellSpacing(6);
	    layout.setWidth("500px");
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
	    
	    // Add a title to the form
	    layout.setHTML(0, 0, "<b>Temporal Coverage</b><br>");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	 // Add some standard form options
	    layout.setHTML(1, 0, "Start Date");
	    layout.setWidget(1, 1, startDateTB);
	    layout.setHTML(2, 0, "End Date");
	    layout.setWidget(2, 1, endDateTB);
	    
	    
	    cadastroBtn = new Button();
	    
	    cadastroBtn.setText("OK");
		
		cellFormatter.setHorizontalAlignment(9, 1,HasHorizontalAlignment.ALIGN_CENTER);
		cadastroBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				DateTimeFormat novo = null;
				novo = novo.getFormat("dd/MM/yyyy");
				temporalCoverage.setEndDate(novo.parse(endDateTB.getText()));
				temporalCoverage.setStartDate(novo.parse(startDateTB.getText()));
				greetingService.setTemporalCoverage(temporalCoverage, BrazilianIPT.keyBD, 
						new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						
					}
					
					@SuppressWarnings("unchecked")
					public void onSuccess(String result) {
						BrazilianIPT.metadataMenu.getTemporalCoverageLk().setEnabled(false);
						BrazilianIPT.keyBD = result;
						BrazilianIPT.formComplete++;
						BrazilianIPT.showNextPanel();
						
						
					}
				});
				
			}
		});
		layout.setWidget(10, 0, cadastroBtn);
		cellFormatter.setColSpan(10, 0, 4);
		cellFormatter.setHorizontalAlignment(10, 0,HasHorizontalAlignment.ALIGN_CENTER);
	    // Wrap the contents in a DecoratorPanel
	    DecoratorPanel decPanel = new DecoratorPanel();
	    decPanel.setWidget(layout);
	    return decPanel;
	}

	public Widget getCadastroBtn() {
		return cadastroBtn;
	}

	public void setMetaData(MetaData metaData) {
		this.metaData = metaData;
	}


	public MetaData getMetaData() {
		return metaData;
	}

	
	
}
